package com.dswz.utils;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class HttpClientUtil {
	private static Logger logger = LogManager.getLogger(HttpClientUtil.class);
	public static final Integer ConnectTimeout = Integer.valueOf(2000);
	public static final Integer ConnectionRequestTimeout = Integer.valueOf(5000);
	public static final Integer SocketTimeout = Integer.valueOf(10000);
	public static final Boolean StaleConnectionCheckEnabled = Boolean.valueOf(true);

	public static String doPost(String url, Map<String, String> params) {
		return null;

	}

	public static String doGet(String url) {
		CloseableHttpClient httpCilent = HttpClients.createDefault();// Creates CloseableHttpClient instance with //
																		// default configuration.
		HttpGet httpGet = new HttpGet(url);
		try {
			httpCilent.execute(httpGet);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		} finally {
			try {
				httpCilent.close();// 释放资源
			} catch (IOException e) {
				logger.error(e.getMessage(), e);
			}
		}
		return null;
	}

	public static String doGet(String url, Map<String, String> params) {
		URIBuilder builder = null;
		try {
			builder = new URIBuilder(url);
			for (Map.Entry<String, String> entry : params.entrySet()) {
				builder.setParameter((String) entry.getKey(), (String) entry.getValue());
			}
			return doGet(builder.build().toString());
		} catch (URISyntaxException e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	public static String doGet2(String url) {
		try {
			// 根据地址获取请求
			HttpGet request = new HttpGet(url);// 这里发送get请求
			// 获取当前客户端对象
			DefaultHttpClient httpClient = new DefaultHttpClient();
			// 通过请求对象获取响应对象
			HttpResponse response = httpClient.execute(request);

			// 判断网络连接状态码是否正常(0--200都数正常)
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				String result = EntityUtils.toString(response.getEntity(), "utf-8");
				System.out.println("result:" + result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String doGet2(String url, Map<String, String> params) {
		URIBuilder builder = null;
		try {
			builder = new URIBuilder(url);
			for (Map.Entry<String, String> entry : params.entrySet()) {
				builder.setParameter((String) entry.getKey(), (String) entry.getValue());
			}
			return doGet2(builder.build().toString());
		} catch (URISyntaxException e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	public static String upload() throws Exception {
		CloseableHttpClient httpClient = null;
		try {
			httpClient = HttpClients.createDefault();
			HttpPost httpPost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceFile.action");

			FileBody bin = new FileBody(new File("F:\\image\\sendpix0.jpg"));
			StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
			HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("bin", bin).addPart("comment", comment)
					.build();
			httpPost.setEntity(reqEntity);
			CloseableHttpResponse response = httpClient.execute(httpPost);

			String content = EntityUtils.toString(response.getEntity(), "UTF-8");
			return content;
		} catch (Exception e) {
			throw e;
		} finally {
			if (httpClient != null) {
				httpClient.close();
			}
		}
	}

	public static RequestConfig getRequestConfig() {
		RequestConfig config = RequestConfig.custom().setConnectTimeout(ConnectTimeout.intValue())
				.setConnectionRequestTimeout(ConnectionRequestTimeout.intValue())
				.setSocketTimeout(SocketTimeout.intValue())
				.setStaleConnectionCheckEnabled(StaleConnectionCheckEnabled.booleanValue())/* 216 */ .build();
		return config;
	}
}
